package com.thesis.manage.common.redis.impl;

import com.thesis.manage.common.redis.RedisDao;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author php
 * @date 2017/11/30
 */
@Component
public class RedisDaoImpl implements RedisDao {

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public void rpush(String key, String... value) {
        this.stringRedisTemplate.opsForList().rightPushAll(key, value);
    }

    @Override
    public void lpush(String key, String... value) {
        this.stringRedisTemplate.opsForList().leftPushAll(key, value);
    }

    @Override
    public Long llen(String key) {
        return this.stringRedisTemplate.opsForList().size(key);
    }

    @Override
    public List lrange(String key, int start, int end) {
        return this.stringRedisTemplate.opsForList().range(key, start, end);
    }

    @Override
    public void lset(String key, int index, String value) {
        this.stringRedisTemplate.opsForList().set(key, index, value);
    }

    @Override
    public String lpop(String key) {
        return this.stringRedisTemplate.opsForList().leftPop(key);
    }

    @Override
    public String rpop(String key) {
        return this.stringRedisTemplate.opsForList().rightPop(key);
    }

    @Override
    public boolean exists(String key) {

        return this.stringRedisTemplate.hasKey(key);
    }

    @Override
    public void del(String key) {
        this.stringRedisTemplate.delete(key);
    }

    @Override
    public void set(String key, String value) {
        this.stringRedisTemplate.opsForValue().set(key, value);
    }

    @Override
    public String get(String key) {
        return this.stringRedisTemplate.opsForValue().get(key);
    }

    @Override
    public List multiGet(List<String> keys) {
        return this.stringRedisTemplate.opsForValue().multiGet(keys);
    }

    @Override
    public String getAndSet(String key, String value) {
        return this.stringRedisTemplate.opsForValue().getAndSet(key, value);
    }

    @Override
    public void lrem(String key, Integer count, String value) {
        this.stringRedisTemplate.opsForList().remove(key, count, value);
    }

    @Override
    public void sadd(String key, String value) {
        this.stringRedisTemplate.opsForSet().add(key, value);
    }

    @Override
    public String spop(String key) {
        return this.stringRedisTemplate.opsForSet().pop(key);
    }

    @Override
    public Set<String> smembers(String key) {
        return this.stringRedisTemplate.opsForSet().members(key);
    }


    @Override
    public Long scard(String key) {
        return this.stringRedisTemplate.opsForSet().size(key);
    }

    @Override
    public void expire(String key, int value) {
        this.stringRedisTemplate.expire(key, value, TimeUnit.SECONDS);
    }

    @Override
    public void setex(String key, int expireTime, String value) {
        this.stringRedisTemplate.opsForValue().set(key, value, expireTime, TimeUnit.SECONDS);
    }

    @Override
    public void srem(String key, String value) {
        this.stringRedisTemplate.opsForSet().remove(key, value);
    }

    @Override
    public boolean sismember(String key, String value) {
        return this.stringRedisTemplate.opsForSet().isMember(key, value);
    }

    @Override
    public Long incr(String key) {
        return this.stringRedisTemplate.opsForValue().increment(key, 1);
    }

    @Override
    public Long hdelete(String key, String hashKey) {
        return this.stringRedisTemplate.opsForHash().delete(key, hashKey);
    }

    @Override
    public boolean hasKey(String key, String hashKey) {
        return this.stringRedisTemplate.opsForHash().hasKey(key, hashKey);
    }

    @Override
    public Object hget(String key, String hashKey) {
        return this.stringRedisTemplate.opsForHash().get(key, hashKey);
    }

    @Override
    public List<Object> hMulityGet(String key, List<Object> hashKeys) {
        return this.stringRedisTemplate.opsForHash().multiGet(key, hashKeys);
    }

    @Override
    public Set<Object> hkeys(String key) {
        return this.stringRedisTemplate.opsForHash().keys(key);
    }

    @Override
    public Long hsize(String key) {
        return this.stringRedisTemplate.opsForHash().size(key);
    }

    @Override
    public void hput(String key, String hashKey, Object value) {
        this.stringRedisTemplate.opsForHash().put(key, hashKey, value);
    }

    @Override
    public void hputAll(String key, Map<? extends String, ?> keyAndValue) {
        this.stringRedisTemplate.opsForHash().putAll(key, keyAndValue);
    }

    @Override
    public Map<Object, Object> hEntries(String key) {
        return this.stringRedisTemplate.opsForHash().entries(key);
    }
}
